package wsepForum.common.elements;

import java.util.Vector;

/**
 *
 * @author Ofir Tam & Ori Ossmy
 *
 */
public class Section extends SectionHeader {

	/*****************************
	 * MEMBERS                   *
	 *****************************/
	/**
	 *
	 */
	private Vector<SectionHeader> subSections;

	/**
	 *
	 */
	private Vector<TopicHeader> topics;


	/*****************************
	 * CONSTRCTORS               *
	 *****************************/

	/**
	 *
	 * @param sectionHeadLine
	 */
	public Section(String sectionHeadLine, int parentID) {
		super(sectionHeadLine, parentID);
		this.subSections = new Vector<SectionHeader>();
		this.topics = new Vector<TopicHeader>();
	}



	/*****************************
	 * METHODS                   *
	 *****************************/
	/**
	 *
	 * @return
	 */
	public Vector<SectionHeader> getAllSectionHeaders(){
		return this.subSections;
	}

	/**
	 *
	 * @return
	 */
	public Vector<TopicHeader> getAllTopicHeaders(){
		return this.topics;
	}

	/**
	 *
	 * @param index
	 * @return
	 */
	public SectionHeader getSectionHeader(int index){
		return this.subSections.elementAt(index);
	}

	/**
	 *
	 * @param index
	 * @return
	 */
	public TopicHeader getTopicHeader(int index){
		return this.topics.elementAt(index);
	}

	/**
	 *
	 * @param sectionHeader
	 */
	public void addSectionHeader(SectionHeader sectionHeader){
		this.handleSections(sectionHeader.getElementID());
		sectionHeader.setParentID(this.getElementID());
		this.subSections.add(sectionHeader);
	}

	/**
	 *
	 * @param topicHeader
	 */
	public void addTopicHeader(TopicHeader topicHeader){
		this.handleTopics(topicHeader.getElementID());
		topicHeader.setParentID(this.getElementID());
		this.topics.add(topicHeader);
	}

	/**
	 *
	 * @param sectionHeader
	 */
	public void removeSectionHeader(Section section){
		this.handleSections(section.getElementID());
	}

	/**
	 *
	 * @param topicHeader
	 */
	public void removeTopicHeader(Topic topic){
		this.handleTopics(topic.getElementID());
	}


	/**
	 *
	 * @param headers
	 * @param newHeader
	 */
	private void handleSections(int id) {
		boolean isContained = false;
		for (int tIndex = 0; ((!isContained)&&(tIndex < this.subSections.size())); tIndex++) {
			if (this.subSections.elementAt(tIndex).getElementID()==id) {
				isContained=true;
				this.subSections.removeElementAt(tIndex);
			}
		}
	}
	
	
	/**
	 *
	 * @param headers
	 * @param newHeader
	 */
	private void handleTopics(int id) {
		boolean isContained = false;
		for (int tIndex = 0; ((!isContained)&&(tIndex < this.topics.size())); tIndex++) {
			if (this.topics.elementAt(tIndex).getElementID()==id) {
				isContained=true;
				this.topics.removeElementAt(tIndex);
			}
		}
	}
	
}
